using System;
using System.Reflection;
using System.Runtime.Remoting;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;

namespace MyXmlRPCRemote
{
	public class TrayApp
	{
		private NotifyIcon _notifyIcon1;
        private ContextMenuStrip _contextMenuStrip;
        private ToolStripMenuItem _startTool;
        private ToolStripMenuItem _stopTool;
		private ToolStripMenuItem _quitTool;
		private ToolStripLabel _statusLabel;
		
		private Configuration _Config;
		private Service _TheService;
		
		public TrayApp ()
		{
			_notifyIcon1 = new NotifyIcon();
			Bitmap bm = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MyXmlRPCRemote.remote_control.png"));
            _notifyIcon1.Icon = Icon.FromHandle(bm.GetHicon());
			_notifyIcon1.Visible = true;

            _contextMenuStrip = new ContextMenuStrip();
			_statusLabel = new ToolStripLabel();
            _startTool = new ToolStripMenuItem();
            _stopTool = new ToolStripMenuItem();
			_quitTool = new ToolStripMenuItem();
			

            _contextMenuStrip.Items.AddRange(new ToolStripItem[] {
			_statusLabel,
			new ToolStripSeparator(),
            _startTool,
            _stopTool,
			_quitTool});
			
			_statusLabel.Name = "statusLabel";
			_statusLabel.Text = "stopped";
            
			bm = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MyXmlRPCRemote.start.png"));
            _startTool.Name = "startItem";
            _startTool.Text = "start";
			_startTool.Image = bm;
            
			bm = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MyXmlRPCRemote.stop.png"));
            _stopTool.Name = "stopItem";
            _stopTool.Text = "stop";
			_stopTool.Image = bm;
			_stopTool.Enabled = false;
			
			bm = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MyXmlRPCRemote.quit.png"));
			_quitTool.Name = "quitItem";
            _quitTool.Text = "quit";
			_quitTool.Image = bm;
			
            _startTool.Click += new System.EventHandler(this.StartServer_Click);
			_stopTool.Click += new System.EventHandler(this.StopServer_Click);
			_quitTool.Click += new System.EventHandler(this.QuitServer_Click);

            _notifyIcon1.ContextMenuStrip = _contextMenuStrip;
			
			_Config = null;
			_TheService = null;
			RemotingConfiguration.Configure("MyXmlRPCRemote.exe.config", false);
		}
		
		public void LoadConfig() {
			string homePath = (Environment.OSVersion.Platform == PlatformID.Unix || 
                   Environment.OSVersion.Platform == PlatformID.MacOSX) ? Environment.GetEnvironmentVariable("HOME")
    				: Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
			_Config = new Configuration(homePath + "/.myxmlrpcremote/commands.xml");
		}
		
		private void StartServer_Click(object sender, EventArgs e)
        {
			if (_TheService == null) {
				try {
					_TheService = _Config.ReadConfig();
					RemotingServices.Marshal(_TheService, "MyXmlRPCRemote.rem", typeof(Service));
					_statusLabel.Text = "running";
					SetMenuStatus(false);
				} catch(Exception ex) {
					_TheService = null;
					_statusLabel.Text = "stopped";
					SetMenuStatus(true);
					MessageBox.Show("Start failed.", "Error: " + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			} else {
				MessageBox.Show("Service is already running.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
		
		private void StopServer_Click(object sender, EventArgs e)
        {
			if (_TheService != null) {
				try {
					RemotingServices.Disconnect(_TheService);
				} finally {
					_TheService = null;
					_statusLabel.Text = "stopped";
					SetMenuStatus(true);
				}
			} else {
				MessageBox.Show("Service is not running.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
		
		private void QuitServer_Click(object sender, EventArgs e)
        {
			Application.Exit();
		}
		
		private void SetMenuStatus(bool canStart) {
			_startTool.Enabled = canStart;
			_stopTool.Enabled = !canStart;	
		}
	}
}

